package com.tanzc.compare;

import com.tanzc.sort.*;
import com.tanzc.util.ArrayUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p> description: 比较各种排序
 *
 * 对于有额外空间开销的排序方法，统一采用返回值方式返回新数组，而不是引用参数传递
 *
 * </p>
 *
 * @author tanzc
 * @date 2021/12/27
 **/
public class CompareSpeed {

    private static final BubbleSort BUBBLE_SORT = new BubbleSort();
    private static final QuickSort QUICK_SORT = new QuickSort();
    private static final SelectionSort SELECTION_SORT = new SelectionSort();
    private static final InsertionSort INSERTION_SORT = new InsertionSort();
    private static final ShellSort SHELL_SORT = new ShellSort();
    private static final MergeSort MERGE_SORT = new MergeSort();
    private static final HeapSort HEAP_SORT = new HeapSort();
    private static final CountSort COUNT_SORT = new CountSort();
    private static final RadixSort RADIX_SORT = new RadixSort();
    private static final List<String> SORT_TYPE = new ArrayList<>(Arrays.asList("heap", "merge", "shell", "quick", "count", "radix", "insertion", "selection", "bubble"));
    private static final List<String> SORT_TYPE2 = new ArrayList<>(Arrays.asList("heap", "merge", "shell", "quick", "count", "radix"));

    public static void main(String[] args) {

        int num = 100000000, max = num * 5;
        int[] arr = ArrayUtils.randomoffset(num, max, 100);
//        int[] arr = ArrayUtils.worst(num);
//        int[] arr = ArrayUtils.bad(num);
//        int[] arr = ArrayUtils.nice(num);

        CompareSpeed compareSpeed = new CompareSpeed();
        compareSpeed.compare(arr);

    }



    public void compare(int[] arr) {
        SORT_TYPE2.forEach(sort -> {
            int[] arrtmp = new int[arr.length];
            int[] arrtmp2 = new int[arr.length];
            System.arraycopy(arr, 0, arrtmp, 0, arr.length);
            System.out.print(sort + "-----time: ");
            Date start = new Date();
            switch (sort) {
                case "bubble":
                    BUBBLE_SORT.bubble3(arrtmp);
                    break;
                case "quick":
                    QUICK_SORT.quick(arrtmp, 0, arrtmp.length - 1);
                    break;
                case "selection":
                    SELECTION_SORT.selction(arrtmp);
                    break;
                case "insertion":
                    INSERTION_SORT.insertion(arrtmp);
                    break;
                case "shell":
                    SHELL_SORT.shell(arrtmp);
                    break;
                case "merge":
                    MERGE_SORT.merge(arrtmp);
                    break;
                case "heap":
                    HEAP_SORT.heap(arrtmp);
                    break;
                case "count":
                    COUNT_SORT.count2(arrtmp);
                    break;
                case "radix":
                    RADIX_SORT.radix(arrtmp);
                    break;
                default:
                    break;
            }
            Date end = new Date();
            long timediff = end.getTime() - start.getTime();
            System.out.println(timediff + "ms");
        });
    }
}
